<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-04-04 16:02:26
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
/**
 * uid 会员id
 * username 会员名
 * type 1 为购买记录 2为提现记录
 * exid 购买票时的积分类型
 * number 票据张数
 * money 花费的积分数量或者提现的金额
 *
 */
class table_jamesonread_piao extends discuz_table
{
    public function __construct()
    {
        $this->_table = 'jamesonread_piao';
        $this->_pk    = 'id';
        $this->_pre_cache_key = 'jamesonreadpiao_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    /**
     *申请提现通过加入表
     *
     *
     */
    public function tongguo($uid, $type, $username, $number, $money, $exid)
    {
        if ($res = $this->insert(array(
            'uid'      => $uid,
            'username' => $username,
            'type'     => $type,
            'number'   => $number,
            'money'    => $money,
            'exid'     => $exid,
            'addtime'  => TIMESTAMP,
        ))) {
            return $res;
        } else {
            return 0;
        }
    }
    /**
     * 后台全部购买记录 或提现记录
     */
    public function fetch_buy_all($type, $start, $size)
    {
        return DB::fetch_all('SELECT * FROM %t WHERE type=%d ORDER BY addtime DESC LIMIT %d,%d', array($this->_table, $type, $start, $size));
    }

    public function fetch_by_uid($uid, $type, $start, $size)
    {
        if ($type == 1) {
            return DB::fetch_all('SELECT p.*,b.book_name FROM %t AS p INNER JOIN %t AS b ON p.book_id=b.book_id WHERE p.type=%d AND p.uid=%d ORDER BY p.addtime DESC LIMIT %d,%d', array($this->_table, 'jamesonread_books', $type, $uid, $start, $size));
        }
        return DB::fetch_all('SELECT * FROM %t WHERE type=%d AND uid=%d ORDER BY addtime DESC LIMIT %d,%d', array($this->_table, $type, $uid, $start, $size));
    }
    public function count_by_uid($uid)
    {
        $data = DB::result_first('SELECT count(id) FROM %t WHERE uid=%d AND type=%d', array($this->_table, $uid, 1));
        return $data;
    }
    public function fetch_by_page($uid,$start,$size)
    {
        
        $data = DB::fetch_all('SELECT p.*,b.* FROM %t AS p INNER JOIN %t AS b ON p.book_id=b.book_id WHERE p.uid=%d AND p.type=%d ORDER BY p.addtime DESC LIMIT %d,%d', array($this->_table, 'jamesonread_books', $uid, 1, $start, $size));
        return $data;
    }
}